package com.richapp.launcher.application;

import android.app.AlarmManager;
import android.app.Application;
import android.app.PendingIntent;
import android.content.Context;
import android.content.Intent;
import android.support.multidex.MultiDex;
import android.util.Log;

import com.jack.netty.log.LoggerManager;
import com.richapp.launcher.R;
import com.richapp.launcher.busevent.EventServiceDestroy;
import com.richapp.launcher.database.system.mode.BoxLisence;
import com.richapp.launcher.log.AndroidLogger;
import com.richapp.launcher.service.LocalService;
import com.richapp.launcher.ui.activities.BootAdvActivity;
import com.richapp.launcher.ui.activities.StartActivity;
import com.richapp.launcher.utils.IniUtil;
import com.richapp.launcher.utils.MetaUtil;
import com.richapp.launcher.utils.OsUtil;
import com.richapp.launcher.utils.ServiceUtil;
import com.tencent.bugly.Bugly;
import com.tencent.bugly.beta.Beta;

import java.io.File;
import java.util.concurrent.TimeUnit;

import de.greenrobot.event.EventBus;
//
public class SmartTVApplication extends Application  implements Thread.UncaughtExceptionHandler{

    private String dealers;
    private String vendor;
    private String lisence;
    private static Context instance;
    @Override
    public void onCreate() {
        super.onCreate();
        //// TODO: 2017/7/23  这里 注释了 这里 会吃掉所有异常导致bugly不上报  所以测试时候 最好注释乐乐

//        Thread.setDefaultUncaughtExceptionHandler(this);
        instance = getApplicationContext();

        String processName = OsUtil.getProcessName(this, android.os.Process.myPid());
        if (processName == null) {
            return;
        }
        boolean defaultProcess = processName.equals(getApplicationContext().getPackageName());
        if (!defaultProcess) {
            return;
        }

        MetaUtil.init(getApplicationContext());
        EventBus.getDefault().register(this);

        //android log
        LoggerManager.getInstance().setiLogger(new AndroidLogger());

        File configFile = new File("/system/etc/device_info.ini");
        if (configFile == null || !configFile.exists() || !configFile.canRead()) {

        } else {
            IniUtil iniUtil = new IniUtil(configFile.getAbsolutePath());
            BoxLisence.getInstance().setVendor(iniUtil.get("general", "vendor").get(0));
            BoxLisence.getInstance().setDealers(iniUtil.get("general", "dealers").get(0));
            BoxLisence.getInstance().setLisence(iniUtil.get("general", "license").get(0));
        }

//        playBootAdv();

//        startService();

        LoggerManager.getInstance().getiLogger().info(getClass(), "dealers:" + getDealers());
        LoggerManager.getInstance().getiLogger().info(getClass(), "vendor:" + getVendor());
        LoggerManager.getInstance().getiLogger().info(getClass(), "license:" + getLisence());


        //腾讯bugly 升级
        Beta.autoInit = true;
        Beta.autoCheckUpgrade = true;
        Beta.upgradeCheckPeriod = 10 * 1000;
        Beta.initDelay = 5 * 1000;
        Beta.autoDownloadOnWifi = true;
        Beta.enableNotification = true;
        Beta.strToastCheckingUpgrade="";
        Beta.strToastYourAreTheLatestVersion="";
        Beta.strToastCheckUpgradeError="";


        Bugly.init(this, getResources().getString(R.string.app_id), false);
        Beta.checkUpgrade(false, true);
        startService(new Intent(this, LocalService.class));


    }
    //获取全局context
    public static Context getContext() {
        return instance;
    }

    private void playBootAdv() {
        try {
            TimeUnit.SECONDS.sleep(3);
        } catch (InterruptedException e) {

        }
        Intent intent = new Intent(getApplicationContext(), BootAdvActivity.class);
        intent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
        startActivity(intent);
    }

    public String getDealers() {
        return dealers;
    }

    private void setDealers(String dealers) {
        this.dealers = dealers;
    }

    public String getVendor() {
        return vendor;
    }

    private void setVendor(String vendor) {
        this.vendor = vendor;
    }

    public String getLisence() {
        return lisence;
    }

    private void setLisence(String lisence) {
        this.lisence = lisence;
    }

    public void onEventMainThread(EventServiceDestroy event) {
        ServiceUtil.startService(this, event.getsClass());
    }


    @Override
    protected void attachBaseContext(Context base) {
        super.attachBaseContext(base);
        // you must install multiDex whatever tinker is installed!
        MultiDex.install(base);
        // 安装tinker
        Beta.installTinker();
    }
   //// TODO: 2017/7/23  这里 注释了 这里 会吃掉所有异常导致bugly不上报  所以测试时候 最好注释乐乐
    //@Override
    public void uncaughtException(Thread thread, Throwable ex) {
/*        Log.v("error_launch",ex.getMessage());
        Bugly.putUserData(getApplicationContext(),"error", ex == null?"异常了，没有信息":ex.getMessage());
        Intent intent = new Intent(this, StartActivity.class);
        intent.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP | Intent.FLAG_ACTIVITY_CLEAR_TASK);
        PendingIntent restartIntent = PendingIntent.getActivity(
                getApplicationContext(), 0, intent, PendingIntent.FLAG_ONE_SHOT);
        //重启应用
        AlarmManager mgr = (AlarmManager) getSystemService(Context.ALARM_SERVICE);
        mgr.set(AlarmManager.RTC, System.currentTimeMillis(), restartIntent);
        //退出程序
        android.os.Process.killProcess(android.os.Process.myPid());
        System.exit(0);
        System.gc();*/
    }



}
